from utils import restful
from .models import *
from .serializers import UserSerializer


def login(request):
    username = request.POST.get('username')
    password = request.POST.get('password')

    try:
        user = User.objects.get(username=username, password=password)
        result = UserSerializer(user).data
        return restful.ok(
            message="登陆成功！",
            data=result
        )
    except:
        return restful.params_error(message="账号或密码错误，请重新输入！")


def modify_userinfo(request):
    user_id = request.POST.get('user_id')
    sex = request.POST.get('sex')
    # instructor_id = request.POST.get('instructor_id')
    # student_class = request.POST.get('student_class')
    id_card = request.POST.get('id_card')
    bank_card = request.POST.get('bank_card')
    password = request.POST.get('password', '')
    try:
        info = UserInfo.objects.filter(user_id=user_id)
        info.update(
            **{
                # "instructor_id": instructor_id,
                # 'student_class': student_class,
                'sex': sex,
                'id_card': id_card,
                'bank_card': bank_card
            }
        )
        if password != '':
            user = User.objects.filter(id=user_id).update(password=password)
    except:
        return restful.params_error()

    return restful.ok(message='修改成功')
